package com.zy.gpdemo.service;

import com.gp.command.EscCommand;
import com.zy.gpdemo.domain.Status;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Vector;

public class ESCPrinter {

    public Status status = new Status();
    Internet printer = new Internet();
    EscCommand esc = new EscCommand();
    String ip;
    int port;

    public Status getStatus() {
        PrinterStatus();
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public ESCPrinter(String ip, int port){
        this.ip = ip;
        this.port = port;
    }

    public boolean openNetPort(){
        return printer.openNetPort(ip,port);
    }

    public boolean closeNetPort(){
        return printer.closeNetPort();
    }

    public boolean Connect(){
        try {
            return InetAddress.getByName(ip).isReachable(1000);
        } catch (IOException e) {
            return false;
        }
    }

    public boolean PrinterStatus() {
    	if(!Connect()) {
    		return false;
    	}
        Internet printerStatus = new Internet();
        if(!printerStatus.openNetPort(ip, 4000)) {
        	return false;
        }
        if (!printerStatus.sendMessage(new byte[]{0x1B,0x76}) || !printerStatus.readMessage()) {
            return false;
        }
        printerStatus(printerStatus.readBuffer[0],printerStatus.readBuffer[1],printerStatus.readBuffer[2]);
        printerStatus.closeNetPort();
        return true;
    }

    public boolean printMessage(String message){
        esc.getCommand().clear();
        esc.addInitializePrinter();
        esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);// 设置打印居中
        esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.ON,
                EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF);// 设置为倍高倍宽
        esc.addText(message + "\n"); // 打印文字
        esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF,
                EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);// 取消倍高倍宽
        byte[] bytes = Command(esc.getCommand());
        esc.getCommand().clear();
        boolean res;
        if(openNetPort()) {
        	res = send(bytes);
        	closeNetPort();
        }else {
        	res = false;
        }
        return res;
    }
    
    public boolean printNumber(String message){
        esc.getCommand().clear();
        esc.addInitializePrinter();
        esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);// 设置打印居中
        esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.ON,
                EscCommand.ENABLE.ON, EscCommand.ENABLE.OFF);// 设置为倍高倍宽
        esc.addText(message + "\r\n"); // 打印文字
        esc.addPrintAndFeedLines((byte)5);
        esc.addCutPaper();
        byte[] bytes = Command(esc.getCommand());
        esc.getCommand().clear();
        boolean res;
        if(openNetPort()) {
        	res = send(bytes);
        	closeNetPort();
        }else {
        	res = false;
        }
        return res;
    }

    public boolean send(byte[] bytes) {
        if (printer.sendMessage(bytes)) {
            return true;
        } else {
            System.out.println("发送消息失败: " + ip);
            return false;
        }
    }

    public boolean read(){
        if (printer.readMessage()) {
            return true;
        } else {
            System.out.println("读取打印机数据失败: " + ip);
            return false;
        }
    }

    public byte[] Command(Vector<Byte> command) {
        if (command == null) {
            return null;
        }

        byte[] bytes = new byte[command.size()];

        for (int i = 0; i < command.size(); i++) {
            bytes[i] = command.get(i);
        }
        return bytes;
    }

    public void printerStatus(int first, int second, int third) {

        status.setCloseCashBox((first & 0x04) == 4);

        status.setOffLine((first & 0x08) == 8);

        status.setCover((first & 0x20) == 32);

        status.setFeed((first & 0x40) == 64);

        status.setCutter((second & 0x08) == 8);

        status.setUnRecoverError((second & 0x20) == 32);

        status.setPaperWillDo((third & 0x03) == 3);

        status.setPaperDo((third & 0xC) == 12);

    }

}
